package com.naxiangfish.cn.splash;

import android.content.Intent;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.text.TextUtils;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.naxiangfish.cn.R;
import com.naxiangfish.cn.backface.BackCall;
import com.naxiangfish.cn.naxiangfish.MainActivity;
import com.naxiangfish.cn.weigit.NaXiangProgressView;
import com.naxiangfish.cn.weigit.NaXiangVideoView;
import com.naxiangfish.cn.weigit.NoScrollViewPager;
import com.naxiangfish.cn.weigit.OnFinishListener;

import java.util.List;

/**
 * 作者: 赵航
 * 创建时间: 2018/8/14 14:58
 * 描述:
 */
public class SplashActivityImp implements SplashImpView {
    private SplashActivity mSplashView;
    private String mTime = null;
    public SplashActivityImp(SplashView splashActivity) {
        this.mSplashView = (SplashActivity) splashActivity;
    }
    //加载视频的接口
    @Override
    public void setItViodView(TextView splashtext, NaXiangVideoView mVideoView, NaXiangProgressView splashprogressview) {
        initVido(splashtext,mVideoView,splashprogressview);
    }

    private void initVido(final TextView splashtext, final NaXiangVideoView mVideoView, NaXiangProgressView splashprogressview) {
        mSplashView.getWindow().setFormat(PixelFormat.TRANSLUCENT);
        //保证不息屏
        mSplashView.getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        //播放App视频
        mVideoView.setVideoURI(Uri.parse("android.resource://" + mSplashView.getPackageName() + "/" + R.raw.kr36));
        //播放网络视频
        //        mVideoView.setVideoURI(Uri.parse("http://jzvd.nathen.cn/63f3f73712544394be981d9e4f56b612/69c5767bb9e54156b5b60a1b6edeb3b5-5287d2089db37e62345123a1be272f8b.mp4"));
        //播放本地视频
        //        String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES).getPath()
        //                + "/NaXiang-" + "1534219524559" + ".mp4";
        //        File file = new File(path);
        //        if (!file.exists()){
        //            ToastMake.showToast("本地文件不存在");
        //            return;
        //        }
        //        mVideoView.setVideoPath(file.getAbsolutePath());
        mVideoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                mVideoView.start();
            }
        });
        mVideoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                mVideoView.start();
            }
        });

        //        Color.parseColor(color)
        splashprogressview.setPaintColor(mSplashView.getResources().getColor(R.color.colorAccent));
        splashprogressview.startDownTime(6000, new OnFinishListener() {
            @Override
            public void onFinish(final String time) {
                if (time != null) {
                    if (!TextUtils.equals(mTime, time)) {
                        mSplashView.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (TextUtils.equals(time, "0")) {
                                    splashtext.setText("跳过");
                                } else {
                                    splashtext.setText(time);
                                }
                            }
                        });
                        mTime = time;
                    }
                } else {
                    mSplashView.jmpToMainActivity();
                }
            }
        });
    }

    //加载滑动viewpager
    @Override
    public void initMovePicture(NoScrollViewPager splashpager, List<String> uri, ImageView splashjeempimage) {

        SplashPagerAdapter adapter = new SplashPagerAdapter(mSplashView,uri);
        //点击条目
        adapter.setBackCall(new BackCall() {
            @Override
            public void deal(int which, Object... objects) {

                super.deal(which, objects);
                ImageView imageView = (ImageView) objects[0];

                mSplashView.pressPager(which,imageView);
            }
        });

        splashpager.setAdapter(adapter);

    }

}
